原始笔记

Scaling Up Unit Test

Scaling Up Unit Test

正文

Parameterising Our Unit Tests

Same test code but with different data

Example

from inflammation.models import daily_mean

@pytest.mark.parametrize(
    "test, expected",
    [
        ([ [0, 0], [0, 0], [0, 0] ], [0, 0]),
        ([ [1, 2], [3, 4], [5, 6] ], [3, 4]),
    ])
def test_daily_mean(test, expected):
    """Test mean function works for array of zeroes and positive integers."""
    npt.assert_array_equal(daily_mean(np.array(test)), np.array(expected))

Check the code coverage

install pytest-cov

python3 -m pip install pytest-cov
python3 -m pytest --cov=inflammation.models tests/test_models.py

which statements are not being tested

python3 -m pytest --cov=inflammation.models --cov-report term-missing tests/test_models.py

Switch to English